<?php
 require_once 'connect.php';
 
 if(isset($_GET["page"])){
 	$page = $_GET["page"]-1;//Limit statement in SQL begins from 0
 }else{
 	$page = 0;//Limit statement in SQL begins from 0
 }
 $resultPerPage=10;
 $beginning= $page * $resultPerPage;
 $sth = $pdo->query("select count(*) from news");
 $total= $sth->fetchColumn();

 $sth = $pdo->query("select * from news order by news_id desc limit ".$beginning.",".$resultPerPage."");
 $result = $sth->fetchAll();
 
 foreach ($result as $row){
 	echo '
 	<div class="newsContainer" onclick="gotoNews('.$row["news_id"].');">
 	<h3 class="newsTitle">'.$row["title"].'</h3>
 	<img class="newsImage" src="'.$row["image"].'" border="1"/>
 	<p class="newsContent">
 		'.$row["summary"].'
 	</p>
 	</div>
 	';
 }

 echo "<br/>";
 
 for($a=1;$a <= ceil($total/$resultPerPage) ;$a++){
 	if($a == $page+1){
 		echo "[ ".$a." ]  ";
 	}else{
 		echo "<a href='latestNews.php?page=".$a."'>".$a."</a>  ";
 	}
 	
 }

?>